<template>
	<view class="countCard">
		<view class="count">
			<uni-forms ref="baseForm" :modelValue="dataSource">
				<uni-forms-item label="年龄:">
					<uni-easyinput type="number" v-model="dataSource.age" placeholder="请输入年龄" />
				</uni-forms-item>
				<uni-forms-item label="身高(厘米):">
					<uni-easyinput type="digit" @input="heightInput" v-model="dataSource.height" placeholder="请输入身高" />
				</uni-forms-item>
				<uni-forms-item label="体重(公斤):">
					<uni-easyinput type="digit" @input="weightInput" v-model="dataSource.weight" placeholder="请输入体重" />
				</uni-forms-item>
				<uni-forms-item label="性别:">
					<uni-data-checkbox v-model="dataSource.sex" :localdata="sexs" />
				</uni-forms-item>
			</uni-forms>
		</view>
		<view class="countBtn" @click="getBasalMetabolism">
			<text class="countText">计算</text>
		</view>
		<view class="result">
			<text class="resultText">您的基础代谢为:</text>
			<uni-easyinput class="resultText" v-model="basalMetabolism" />
		</view>
		<view class="describe">
			<text>基础代谢率(BMR):</text>
			<text>在维基百科上定义为:"在自然温带环境中休息时消耗的能量，维持生命所需要的最低能量(意味着人体消化系统不活跃，这需要人类禁食约十二小时)"</text>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		nextTick,
		ref
	} from 'vue'
	const dataSource = reactive({
		sex: 1,
		age: '',
		weight: '',
		height: '',
	})
	const sexs = ref([{
		text: '男',
		value: 1
	}, {
		text: '女',
		value: 2
	}])
	const basalMetabolism = ref()
	//计算基础代谢公式
	const getBasalMetabolism = () => {
		if (dataSource.sex == 1) {
			basalMetabolism.value = (9.99 * dataSource.weight + 6.25 * dataSource.height - 4.92 * dataSource.age + 5)
				.toFixed(0)
		} else if (dataSource.sex == 2) {
			basalMetabolism.value = (9.99 * dataSource.weight + 6.25 * dataSource.height - 4.92 * dataSource.age - 161)
				.toFixed(0)
		}
	}
	// 限制身高输入只能数字加小数点
	const heightInput = (e) => {
		nextTick(() => {
			dataSource.height = e.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
		})

	}
	// 限制体重输入只能数字加小数点
	const weightInput = (e) => {
		nextTick(() => {
			dataSource.weight = e.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
		})
	}
</script>

<style lang="scss">
	.countCard {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.countBtn {
			width: 160rpx;
			height: 70rpx;
			line-height: 70rpx;
			border-radius: 40rpx;
			background-color: #87C38F;
			text-align: center;
			text-indent: 20rpx;
			letter-spacing: 20rpx;
			margin: 20rpx 0;

			.countText {
				font-size: 34rpx;
				color: #fbffee;
				font-weight: bold;
			}
		}

		.result {
			display: flex;
			margin: 20rpx 0;
			font-size: 28rpx;
			color: #606266;
			align-items: center;

			.resultText {
				padding: 0 10rpx;
			}
		}
	}

	.count {
		display: flex;
		justify-content: center;
		margin: 20rpx;
		margin-top: 80rpx;

		.uni-forms {
			width: 600rpx;
		}

		.uni-forms-item__label {
			width: 180rpx !important;
		}
	}

	.describe {
		margin: 20rpx;
		font-size: 24rpx;
		color: #909399;
	}
</style>